﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UIT_CV_Management
{
    public partial class UserControlDaoTaoDaiHoc : UserControl
    {
        private Database myDatabase = Database.shareDatabase;
        bool isEveryThingOK = false;

        // bool for every control
        bool isTuNgayOK = true;
        bool isDenNgayOK = true;
        bool isTenTruongOK = false;
        bool isTenThanhPhoOK = false;
        bool isTenLuanAnOK = false;
        bool isNgayBaoVeOK = true;
        bool isNoiBaoVeOK = false;
        bool isNguoiHuongDanOK = false;
        bool isKetQuaLuanAnOK = false;
        bool isTenTieuLuanOK = false;
        bool isKetQuaTieuLuanOK = false;
        bool isNganhHocOK = false;
        
        // List usercontrol
        List<UserControlMonThiTotNghiep> listUCMonThi = new List<UserControlMonThiTotNghiep>();

        // Object
        List<MonThiTotNghiep> listMonThiTotNghiep = new List<MonThiTotNghiep>();
        TotNghiep totNghiep;
        TieuLuan tieuLuan;
        ChiTietNoiHoc chiTietNoiHoc;
        CheDoHoc cheDoHoc;
        DaoTaoDaiHoc daoTaoDaiHoc;


        public UserControlDaoTaoDaiHoc()
        {
            InitializeComponent();

            // hide check image
            this.hiddenAllErrorImage();

            // init language for lbl
            this.initLanguage();

            // init information for conbobox
            this.initCombobox();

            // un-check checkbox
            checkBoxBaoVeLuanAn.Checked = true;
            checkBoxThiTotNghiep.Checked = false;
            this.lplMonThiTotNghiep.Padding = new Padding(0, 0, SystemInformation.VerticalScrollBarWidth, 0);
        }

        private void hiddenAllErrorImage()
        {
            pictureBoxTuNgay.Visible = false;
            pictureBoxDenNgay.Visible = false;
            pictureBoxTenTruong.Visible = false;
            pictureBoxTenTP.Visible = false;
            pictureBoxTenLuanAn.Visible = false;
            pictureBoxNoiBaoVe.Visible = false;
            pictureBoxKetQuaLuanAn.Visible = false;
            pictureBoxNguoiHuongDan.Visible = false;
            pictureBoxTenTieuLuan.Visible = false;
            pictureBoxKetQuaTieuLuan.Visible = false;
            pictureBoxTenMonThi.Visible = false;
            pictureBoxKetQuaMonThi.Visible = false;

            pictureBoxNganhHoc.Visible = false;
                pictureBoxNgayBaoVe.Visible = false;
        }

        private void initLanguage()
        {
            lblCheDoHoc.Text = Language._("DAIHOC_FORM_CHEDOHOC_LBL");
            lblTuNgay.Text = Language._("DAIHOC_FORM_TUNGAY_LBL");
            lblDenNgay.Text = Language._("DAIHOC_FORM_DENNGAY_LBL");
            lblTenTruong.Text = Language._("DAIHOC_FORM_TENTRUONG_LBL");
            lblTenTP.Text = Language._("DAIHOC_FORM_TENTP_LBL");
            lblNganhHoc.Text = Language._("DAIHOC_FORM_NGANHHOC_LBL");
            checkBoxBaoVeLuanAn.Text = Language._("DAIHOC_FORM_CHECKBOXBAOVELUANAN_LBL");
            lblTenLuanAn.Text = Language._("DAIHOC_FORM_TENLUANAN_LBL");
            lblNoiBaoVe.Text = Language._("DAIHOC_FORM_NOIBAOVE_LBL");
            lblNgayBaoVe.Text = Language._("DAIHOC_FORM_NGAYBAOVE_LBL");
            lblKetQuaLuanAn.Text = Language._("DAIHOC_FORM_KETQUALUANAN_LBL");
            lblNguoiHuongDan.Text = Language._("DAIHOC_FORM_NGUOIHUONGDAN_LBL");
            checkBoxThiTotNghiep.Text = Language._("DAIHOC_FORM_CHECKBOXTHITOTNGHIEP_LBL");
            lblTenTieuLuan.Text = Language._("DAIHOC_FORM_TENTIEULUAN_LBL");
            lblKetQuaMonThi.Text = Language._("DAIHOC_FORM_KETQUAMONTHI_LBL");
            lblKetQua.Text = Language._("DAIHOC_FORM_KETQUA_LBL");
            lblTenMonThi.Text = Language._("DAIHOC_FORM_TENMONTHI_LBL");
        }

        private void initCombobox()
        {
            // get basic info for combobox CheDoHoc
            QueryResult result = myDatabase.Query("SELECT * FROM CheDoHoc");
            if (result.ReturnCode == 200)
            {
                DataTable tableCheDoHoc = result.Table;
                cbxCheDoHoc.DataSource = tableCheDoHoc;
                cbxCheDoHoc.DisplayMember = "TenCheDoHoc";
                cbxCheDoHoc.ValueMember = "MaCheDoHoc";
                cbxCheDoHoc.SelectedIndex = 0;
            }
        }

        private void checkBoxBaoVeLuanAn_CheckedChanged(object sender, EventArgs e)
        {
            // 
            if (checkBoxBaoVeLuanAn.Checked)
            {
                // uncheck checkboxThiTotNghiep
                if (checkBoxThiTotNghiep.Checked == true)
                    checkBoxThiTotNghiep.Checked = false;
                txbTenLuanAn.ReadOnly = false;
                txbNoiBaoVe.ReadOnly = false;
                dtpNgayBaoVe.Enabled = true;
                txbKetQuaLuanAn.ReadOnly = false;
                txbNguoiHuongDan.ReadOnly = false;
            }
            else
            {
                // check checkbox ThiTotNghiep
                if (checkBoxThiTotNghiep.Checked == false)
                    checkBoxThiTotNghiep.Checked = true;
                txbTenLuanAn.ReadOnly = true;
                txbNoiBaoVe.ReadOnly = true;
                dtpNgayBaoVe.Enabled = true;
                txbKetQuaLuanAn.ReadOnly = true;
                txbNguoiHuongDan.ReadOnly = true;
            }
        }

        private void checkBoxThiTotNghiep_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxThiTotNghiep.Checked)
            {
                // uncheck checkbox BaoVeLuanAn
                if (checkBoxBaoVeLuanAn.Checked == true)
                    checkBoxBaoVeLuanAn.Checked = false;
                txbTenTieuLuan.ReadOnly = false;
                txbKetQuaTieuLuan.ReadOnly = false;
                lplMonThiTotNghiep.Enabled = true;
                btnThemMonThi.Enabled = true;
            }
            else
            {
                // check checkbox BaoVeLuanAn
                if (checkBoxBaoVeLuanAn.Checked == false)
                    checkBoxBaoVeLuanAn.Checked = true;
                txbTenTieuLuan.ReadOnly = true;
                txbKetQuaTieuLuan.ReadOnly = true;
                lplMonThiTotNghiep.Enabled = false;
                btnThemMonThi.Enabled = false;
            }
        }

        private void dtpTuNgay_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;
            if (dtpTuNgay.Value.Date.CompareTo(dtpDenNgay.Value.Date) > 0)
            {
                isHasError = true;
            }
            if (isHasError)
            {
                isTuNgayOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                dtpTuNgay.BackColor = Color.Tomato;
                utpTip.Value = Language._("DAIHOC_FORM_TIP_TUNGAY_NULL");
                utpTip.Visible = true;
                pictureBoxTuNgay.Visible = false;
            }
            else
            {
                isTuNgayOK = true;
                dtpTuNgay.BackColor = Color.White;
                pictureBoxTuNgay.Visible = true;
                utpTip.Visible = false;
            }
        }

        private void dtpDenNgay_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;
            if (dtpDenNgay.Value.Date.CompareTo(dtpTuNgay.Value.Date) < 0)
            {
                isHasError = true;
            }

            if (isHasError)
            {
                isDenNgayOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                dtpDenNgay.BackColor = Color.Tomato;
                utpTip.Value = Language._("DAIHOC_FORM_TIP_DENNGAY_NULL");
                utpTip.Visible = true;
                pictureBoxDenNgay.Visible = false;
            }
            else
            {
                isDenNgayOK = true;
                dtpDenNgay.BackColor = Color.White;
                pictureBoxDenNgay.Visible = true;
                utpTip.Visible = false;
            }
        }

        private void txbTenTruong_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;

            if (txbTenTruong.Text.Trim() == "")
                isHasError = true;

            if (isHasError)
            {
                isTenTruongOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                txbTenTruong.BackColor = Color.Tomato;
                utpTip.Value = Language._("DAIHOC_FORM_TIP_TENTRUONG_NULL");
                utpTip.Visible = true;
                pictureBoxTenTruong.Visible = false;
            }
            else
            {
                isTenTruongOK = true;
                txbTenTruong.BackColor = Color.White;
                pictureBoxTenTruong.Visible = true;
                utpTip.Visible = false;
            }
        }

        private void txbTenTP_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;

            if (txbTenTP.Text.Trim() == "")
                isHasError = true;

            if (isHasError)
            {
                isTenThanhPhoOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                txbTenTP.BackColor = Color.Tomato;
                utpTip.Value = Language._("DAIHOC_FORM_TIP_TENTP_NULL");
                utpTip.Visible = true;
                pictureBoxTenTP.Visible = false;
            }
            else
            {
                isTenThanhPhoOK = true;
                txbTenTP.BackColor = Color.White;
                pictureBoxTenTP.Visible = true;
                utpTip.Visible = false;
            }
        }

        private void txbNganhHoc_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;

            if (txbNganhHoc.Text.Trim() == "")
                isHasError = true;

            if (isHasError)
            {
                isNganhHocOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                txbNganhHoc.BackColor = Color.Tomato;
                utpTip.Value = Language._("DAIHOC_FORM_TIP_NGANHHOC_NULL");
                utpTip.Visible = true;
                pictureBoxNganhHoc.Visible = false;
            }
            else
            {
                isNganhHocOK = true;
                txbNganhHoc.BackColor = Color.White;
                pictureBoxNganhHoc.Visible = true;
                utpTip.Visible = false;
            }
        }

        private void txbTenLuanAn_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;

            if (txbTenLuanAn.Text.Trim() == "")
                isHasError = true;

            if (isHasError)
            {
                isTenLuanAnOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                txbTenLuanAn.BackColor = Color.Tomato;
                utpTip.Value = Language._("DAIHOC_FORM_TIP_TENLUANAN_NULL");
                utpTip.Visible = true;
                pictureBoxTenLuanAn.Visible = false;
            }
            else
            {
                isTenLuanAnOK = true;
                txbTenLuanAn.BackColor = Color.White;
                pictureBoxTenLuanAn.Visible = true;
                utpTip.Visible = false;
            }
        }

        private void txbNoiBaoVe_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;

            if (txbNoiBaoVe.Text.Trim() == "")
                isHasError = true;

            if (isHasError)
            {
                isNoiBaoVeOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                txbNoiBaoVe.BackColor = Color.Tomato;
                utpTip.Value = Language._("DAIHOC_FORM_TIP_NOIBAOVE_NULL");
                utpTip.Visible = true;
                pictureBoxNoiBaoVe.Visible = false;
            }
            else
            {
                isNoiBaoVeOK = true;
                txbNoiBaoVe.BackColor = Color.White;
                pictureBoxNoiBaoVe.Visible = true;
                utpTip.Visible = false;
            }
        }

        private void dtpNgayBaoVe_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;
            if (dtpNgayBaoVe.Value.Date.CompareTo(dtpTuNgay.Value.Date) < 0)
            {
                isHasError = true;
            }

            if (isHasError)
            {
                isNgayBaoVeOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                dtpNgayBaoVe.BackColor = Color.Tomato;
                utpTip.Value = Language._("DAIHOC_FORM_TIP_NGAYBAOVE_NULL");
                utpTip.Visible = true;
                pictureBoxNgayBaoVe.Visible = false;
            }
            else
            {
                isNgayBaoVeOK = true;
                dtpNgayBaoVe.BackColor = Color.White;
                pictureBoxNgayBaoVe.Visible = true;
                utpTip.Visible = false;
            }
        }

        private void txbKetQuaLuanAn_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;
            float result;
            if (float.TryParse(txbKetQuaLuanAn.Text, out result) == false)
            {
                isHasError = true;
            }
            else
                if ((result < 0) || (result > 10))
                {
                    isHasError = true;
                }

            if (isHasError)
            {
                isKetQuaLuanAnOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                txbKetQuaLuanAn.BackColor = Color.Tomato;
                utpTip.Value = Language._("DAIHOC_FORM_TIP_KETQUALUANAN_NULL");
                utpTip.Visible = true;
                pictureBoxKetQuaLuanAn.Visible = false;
            }
            else
            {
                isKetQuaLuanAnOK = true;
                txbKetQuaLuanAn.BackColor = Color.White;
                pictureBoxKetQuaLuanAn.Visible = true;
                utpTip.Visible = false;
            }
        }

        private void txbNguoiHuongDan_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;

            if (txbNguoiHuongDan.Text.Trim() == "")
                isHasError = true;

            if (isHasError)
            {
                isNguoiHuongDanOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                txbNguoiHuongDan.BackColor = Color.Tomato;
                utpTip.Value = Language._("DAIHOC_FORM_TIP_NGUOIHUONGDAN_NULL");
                utpTip.Visible = true;
                pictureBoxNguoiHuongDan.Visible = false;
            }
            else
            {
                isNguoiHuongDanOK = true;
                txbNguoiHuongDan.BackColor = Color.White;
                pictureBoxNguoiHuongDan.Visible = true;
                utpTip.Visible = false;
            }
        }

        private void txbTenTieuLuan_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;

            if (txbTenTieuLuan.Text.Trim() == "")
                isHasError = true;

            if (isHasError)
            {
                isTenTieuLuanOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                txbTenTieuLuan.BackColor = Color.Tomato;
                utpTip.Value = Language._("DAIHOC_FORM_TIP_TENTIEULUAN_NULL");
                utpTip.Visible = true;
                pictureBoxTenTieuLuan.Visible = false;
            }
            else
            {
                isTenTieuLuanOK = true;
                txbTenTieuLuan.BackColor = Color.White;
                pictureBoxTenTieuLuan.Visible = true;
                utpTip.Visible = false;
            }
        }

        private void txbKetQuaTieuLuan_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;
            float result;
            if (float.TryParse(txbKetQuaTieuLuan.Text, out result) == false)
            {
                isHasError = true;
            }
            else
                if ((result < 0) || (result > 10))
                {
                    isHasError = true;
                }

            if (isHasError)
            {
                isKetQuaTieuLuanOK = false;

                // change NoiSinh's background color to Tomato
                // Add UserTip
                txbKetQuaTieuLuan.BackColor = Color.Tomato;
                utpTip.Value = Language._("DAIHOC_FORM_TIP_KETQUATIEULUAN_NULL");
                utpTip.Visible = true;
                pictureBoxKetQuaLuanAn.Visible = false;
            }
            else
            {
                isKetQuaTieuLuanOK = true;
                txbKetQuaTieuLuan.BackColor = Color.White;
                pictureBoxKetQuaLuanAn.Visible = true;
                utpTip.Visible = false;
            }
        }

        /// <summary>
        /// Thêm 1 UCMonThiTOtNghiep 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnThemMonThi_Click(object sender, EventArgs e)
        {
            if (checkBoxThiTotNghiep.Checked)
            {
                UserControlMonThiTotNghiep userControllMonThiTotNghiep = new UserControlMonThiTotNghiep();
                userControllMonThiTotNghiep.Anchor = AnchorStyles.Left | AnchorStyles.Right;
                lplMonThiTotNghiep.Controls.Add(userControllMonThiTotNghiep);
                listUCMonThi.Add(userControllMonThiTotNghiep);

            }
        }

        /// <summary>
        /// Kiểm tra toàn bộ UCMonThiTOtNghiep điều không có lỗi
        /// </summary>
        /// <returns></returns>
        private Boolean isListMonThiOK()
        {
            Boolean OK = true;
            foreach (UserControlMonThiTotNghiep monThi in listUCMonThi)
            {
                OK = OK && monThi.isMonThiOK();
            }
            return OK;
        }

        public bool isDaiHocOK()
        {
            // Validating
            dtpTuNgay_Validating(dtpTuNgay, null);
            dtpDenNgay_Validating(dtpDenNgay, null);
            txbTenTruong_Validating(txbTenTruong, null);
            txbTenTP_Validating(txbTenTP, null);
            txbNganhHoc_Validating(txbNganhHoc, null);
            if (checkBoxBaoVeLuanAn.Checked)
            {
                txbTenLuanAn_Validating(txbTenLuanAn, null);
                txbNoiBaoVe_Validating(txbNoiBaoVe, null);
                dtpNgayBaoVe_Validating(dtpNgayBaoVe, null);
                txbKetQuaLuanAn_Validating(txbKetQuaLuanAn, null);
                txbNguoiHuongDan_Validating(txbNguoiHuongDan, null);
            }
            else
            {
                txbTenTieuLuan_Validating(txbTenTieuLuan, null);
                txbKetQuaTieuLuan_Validating(txbKetQuaTieuLuan, null);
            }
            bool checkOne = isTenLuanAnOK && isNoiBaoVeOK && isNgayBaoVeOK && isKetQuaLuanAnOK && isNguoiHuongDanOK;
            bool checkTwo = isTenTieuLuanOK && isKetQuaTieuLuanOK;
            if (isTuNgayOK && isDenNgayOK && isTenTruongOK && isTenThanhPhoOK && isNganhHocOK && (checkOne || checkTwo))
                isEveryThingOK = true;
            else
                isEveryThingOK = false;

            // check list monthi
            if (checkBoxThiTotNghiep.Checked)
                 isEveryThingOK = isEveryThingOK && this.isListMonThiOK();

            if (isEveryThingOK)
                return true;
            else
                return false;
        }

        public DaoTaoDaiHoc getInfoDaoTaoDaiHoc()
        {
            // check everything is OK
            if (this.isDaiHocOK())
            {
                // init object with ThiTotNghiep
                if (checkBoxThiTotNghiep.Checked)
                {
                    // init MonThi object
                    for (int i = 0; i < listUCMonThi.Count; i++)
                    {
                        UserControlMonThiTotNghiep UCMonThi = listUCMonThi[i];
                        MonThiTotNghiep monThi = new MonThiTotNghiep(UCMonThi.TenMonThi, UCMonThi.DiemThi, i);

                        // add to list Monthi
                        listMonThiTotNghiep.Add(monThi);
                    }

                    // inti TotNghiep object
                    totNghiep = new TotNghiep(txbTenTieuLuan.Text, float.Parse(txbKetQuaTieuLuan.Text), listMonThiTotNghiep);

                }
                // init object for TieuLuan
                else
                {
                    // init Tieuluan object
                    tieuLuan = new TieuLuan(txbTenLuanAn.Text, dtpNgayBaoVe.Value, txbNoiBaoVe.Text, float.Parse(txbKetQuaLuanAn.Text)
                    , txbNguoiHuongDan.Text);

                }


                //init CheDoHoc
                cheDoHoc = CheDoHoc.cheDoHocFormID(cbxCheDoHoc.SelectedValue.ToString());

                // init ChiTietNoiHoc 
                chiTietNoiHoc = new ChiTietNoiHoc(txbTenTruong.Text, txbTenTP.Text, txbNganhHoc.Text, dtpTuNgay.Value, dtpDenNgay.Value,
                        tieuLuan, totNghiep, cheDoHoc);

                // init DaoTaoDaiHoc
                daoTaoDaiHoc = new DaoTaoDaiHoc(chiTietNoiHoc);

                return daoTaoDaiHoc;
            }
            return null;
        }

        private void cbxCheDoHoc_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
